package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by phazel on 12/04/14.
 */
public class Library
{
    ArrayList<String> books = new ArrayList<String>();
    ArrayList<String> checkedOutBooks = new ArrayList<String>();

    ArrayList<Movie> movies = new ArrayList<Movie>();
    ArrayList<Movie> checkedOutMovies = new ArrayList<Movie>();

    ArrayList<String> bookBorrowers = new ArrayList<String>();

    User currentUser;

    public Library() {
        books.add("The Agile Samurai - Jonathan Rasmusson");
        books.add("Debt: The first 5,000 years - David Graeber");
        books.add("Building Harlequin's Moon - Larry Niven and Brenda Cooper");

        movies.add(new Movie("Exam", 2009, "Stuart Hazeldine", "8/10"));
        movies.add(new Movie("Jurassic Park", 1993, "Steven Spielberg", "7/10"));
        movies.add(new Movie("Frozen", 2013, "Chris Buck and Jennifer Lee", "unrated"));
    }

    public void listBooks()
    {
        System.out.println( "\n\tAvailable Books" );

        for(int i = 0; i < books.size(); i++)
        {
            System.out.println(i + ". " + books.get( i ));
        }
    }

    public void listMovies()
    {
        System.out.println( "\n\tAvailable Movies" );

        for(int i = 0; i < movies.size(); i++)
        {
            System.out.println(i + ". " + movies.get( i ) + "\n");
        }
    }

    public void listCheckedOutBooks()
    {
        System.out.println( "\n\tBooks currently checked out:" );

        for(int i = 0; i < checkedOutBooks.size(); i++)
        {
            System.out.print(i + ". " + checkedOutBooks.get( i ));
            System.out.println( "\t\tborrowed by: " + currentUser.getName() );
        }
    }

    public void checkoutBook( User user )
    {
        currentUser = user;
        Scanner kb = new Scanner(System.in);

        while(true)
        {
            System.out.print("Select a book's number to check it out: ");

            String input = kb.nextLine();
            int selection = Integer.parseInt(input);

            try
            {
                if ( selection == -1 )
                {
                    System.out.println( "Quitting checkout" );
                    break;
                }
                String book = books.get( selection );
                checkedOutBooks.add( book );
                books.remove( selection );
                System.out.println("Thank you! Enjoy the book");
                break;
            }
            catch( Exception e )
            {
                System.out.println("That book is not available.");
            }
        }
    }

    public void checkoutMovie()
    {
        Scanner kb = new Scanner(System.in);

        while(true)
        {
            System.out.print("Select a movie's number to check it out: ");

            String input = kb.nextLine();
            int selection = Integer.parseInt(input);

            try
            {
                if ( selection == -1 )
                {
                    System.out.println( "Quitting checkout" );
                    break;
                }
                Movie movie = movies.get( selection );
                checkedOutMovies.add( movie );
                movies.remove( selection );
                System.out.println("Thank you! Enjoy the movie");
                break;
            }
            catch( Exception e )
            {
                System.out.println("That movie is not available.");
            }
        }
    }

    public void returnBook()
    {
        listCheckedOutBooks();
        Scanner kb = new Scanner(System.in);

        while(true)
        {
            System.out.print( "\nSelect a book's number to return it: " );
            String input = kb.nextLine();
            int selection = Integer.parseInt(input);

            try
            {
                if ( selection == -1 )
                {
                    System.out.println( "Quitting book return" );
                    break;
                }
                String book = checkedOutBooks.get( selection );
                books.add( book );
                checkedOutBooks.remove( selection );
                System.out.println( "Thank you for returning the book." );
                break;
            }
            catch( Exception e )
            {
                System.out.println( "That is not a valid book to return." );
            }
        }
    }

    public User getCurrentUser()
    {
        return currentUser;
    }
}
